﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;

using MvcCms.Data;


namespace MvcCmsProjectTracker.Data
{
    public class EFTicketRepository : ITicketRepository
    {
        #region Constructors
        private readonly MvcCmsEntities _entities;

        public EFTicketRepository(IEntitiesProvider entitiesProvider)
        {
            _entities = entitiesProvider.GetEntityContext();
        }
        #endregion
        public void Save()
        {
            _entities.SaveChanges();
        }
        public Ticket CreateTicket(Ticket ticketToCreate)
        {
            _entities.Tickets.AddObject(ticketToCreate);
            _entities.SaveChanges();
            return ticketToCreate;
        }
        public Ticket EditTicket(Ticket ticketToEdit)
        {
            _entities.Tickets.ApplyCurrentValues(ticketToEdit);
            _entities.SaveChanges();
            return ticketToEdit;
        }
        public IQueryable<Ticket> GetTicketsByProject(int projectID)
        {
            return from b in _entities.Tickets
                   .Include("AssignedTo")
                   .Include("CreatedBy")
                  .Include("ProjectStatus")
                   .Include("ProjectPriority")
                   where b.ProjectID == projectID
                   orderby b.LastUpdate descending
                   select b;
        }

        public IQueryable<Ticket> GetMyTickets(Guid userId)
        {
            return from b in _entities.Tickets
                   .Include("ProjectStatus")
                   .Include("ProjectPriority")
                   .Include("AssignedTo")
                   where b.CreatedBy.UserID == userId
                   orderby b.LastUpdate descending
                   select b;
        }

        public IQueryable<Ticket> GetTicketsByPriority(int projectID, int priorityID)
        {
            return from b in _entities.Tickets
                   .Include("AssignedTo")
                   .Include("CreatedBy")
                   where b.ProjectID == projectID
                   && b.PriorityID == priorityID
                   orderby b.LastUpdate descending
                   select b;
        }

        public IQueryable<Ticket> GetTicketsByStatus(int projectID, int statusID)
        {
            return from b in _entities.Tickets
                   .Include("AssignedTo")
                   .Include("CreatedBy")
                   where b.ProjectID == projectID
                   && b.StatusID == statusID
                   orderby b.LastUpdate descending
                   select b;
        }

        public IQueryable<Ticket> GetOpenHelpDeskTickets(int portalId)
        {
            return from b in _entities.Tickets
                   .Include("AssignedTo")
                   .Include("CreatedBy")
                   .Include("ProjectStatus")
                   .Include("ProjectPriority")
                   where b.Project.PortalID==portalId
                   && !b.Closed
                   orderby b.LastUpdate descending
                   select b;
        }

        public Ticket GetTicket(int ticketID)
        {
            return _entities.Tickets
                .Include("CreatedBy")
                .Include("AssignedTo")
                .Include("DeveloperIssue")
                .Include("ProjectStatus")
                .Include("ProjectPriority")
                .SingleOrDefault(b => b.TicketID == ticketID);
        }

        public void DeleteTicket(Ticket ticket)
        {
            _entities.Tickets.DeleteObject(ticket);            
        }

        public void AddTicket(Ticket ticket)
        {
            _entities.Tickets.AddObject(ticket);
        }

        public IQueryable<TicketComment> GetTicketComments(int ticketID)
        {
            return from b in _entities.TicketComments
                   where b.TicketID == ticketID
                   orderby b.DateCreated ascending
                   select b;
        }

        public TicketComment GetTicketComment(int commentID)
        {
            return _entities.TicketComments.SingleOrDefault(b => b.CommentID == commentID);
        }

        public void AddTicketComment(TicketComment comment)
        {
            _entities.TicketComments.AddObject(comment);
        }

        public void DeleteTicketComment(TicketComment comment)
        {
            _entities.TicketComments.DeleteObject(comment);
        }

        public IQueryable<TicketHistory> GetTicketHistories(int ticketID)
        {
            return from h in _entities.TicketHistories
                   where h.TicketID == ticketID
                   orderby h.DateCreated descending
                   select h;
        }

        public TicketHistory GetTicketHistory(int historyID)
        {
            return _entities.TicketHistories.SingleOrDefault(h => h.HistoryID == historyID);
        }

        public void AddTicketHistory(TicketHistory history)
        {
            _entities.TicketHistories.AddObject(history);
        }

        public void DeleteTicketHistory(TicketHistory history)
        {
            _entities.TicketHistories.DeleteObject(history);
        }

        public IQueryable<TicketNotification> GetTicketNotifications(int ticketID)
        {
            return from b in _entities.TicketNotifications
                   where b.TicketID == ticketID
                   select b;
        }

        public TicketNotification GetTicketNotification(int notificationID)
        {
            return _entities.TicketNotifications.SingleOrDefault(n => n.NotificationID == notificationID);
        }

        public void AddTicketNotification(TicketNotification notification)
        {
            _entities.TicketNotifications.AddObject(notification);
        }

        public void DeleteTicketNotification(TicketNotification notification)
        {
            _entities.TicketNotifications.DeleteObject(notification);
        }

        public IQueryable<TicketAttachment> GetTicketAttachments(int ticketID)
        {
            return from a in _entities.TicketAttachments
                   where a.TicketID == ticketID
                   select a;
        }

        public TicketAttachment GetTicketAttachment(int attachmentID)
        {
            return _entities.TicketAttachments.SingleOrDefault(a => a.AttachmentID == attachmentID);
        }

        public void AddTicketAttachment(TicketAttachment attachment)
        {
            _entities.TicketAttachments.AddObject(attachment);
        }

        public void DeleteTicketAttachment(TicketAttachment attachment)
        {
            _entities.TicketAttachments.DeleteObject(attachment);
        }
    }
}